﻿using System.Web.Mvc;
using System;
using System.Web.Routing;

namespace ScoutNET.Attributes
{
    public class NotFoundErrorAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var view = filterContext.Result as ViewResult;

            if (view != null && view.ViewName.ToLower() == "error404")
            {
                filterContext.RequestContext.HttpContext.Response.TrySkipIisCustomErrors = true;
                filterContext.RequestContext.HttpContext.Response.StatusDescription = "Not Found";
                filterContext.RequestContext.HttpContext.Response.StatusCode = 404;

                if (view.ViewData["Breadcrumb"] == null)
                {
                    string[] saBreadcrumb = new String[2];
                    saBreadcrumb[0] = HtmlHelper.GenerateLink(filterContext.Controller.ControllerContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
                    saBreadcrumb[1] = Resources.Error_Error404_Breadcrumb;
                    view.ViewData["Breadcrumb"] = saBreadcrumb;
                }

                if (view.ViewData["PageTitle"] == null)
                {
                    view.ViewData["PageTitle"] = Resources.Error_Error404_Title;
                }

                if (view.ViewData["PageDescription"] == null)
                {
                    view.ViewData["PageDescription"] = Resources.Error_Error404_Description;
                }
            }
            
            base.OnResultExecuting(filterContext);
        }
    }
}